import type { Request, Response } from "express";
import { handleError, handleSucceed } from "@/utils/stateHandle";
import { JWT } from "@/utils/JWT";

export const list = (req: Request, res: Response) => {
  const { page, limit, value } = req.body;
  const newPage = page - 1;
  let list = [];

  try {
    // let list = []
    for (let i = 0; i < 100; i++) {
      list.push({
        id: i,
        name: `数据：${i}`,
        value: (i % 2).toString(),
      });
    }

    // 数据过滤
    list = list.filter((item) => item.name?.indexOf(value) !== -1);

    const total = list.length;

    // 返回需要的数据
    const data = list?.slice(newPage * limit, newPage * limit + limit) || [];

    const send = {
      list: data, // 数据
      page, // 当前分页
      limit, // 条数
      total, // 总条数
    };

    handleSucceed(res, send, "获取成功");
  } catch (error) {
    handleError(res, error);
  }
};
